Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | 1x 1x 1x 144x 144x 144x 144x 129x 129x 144x 4x 4x 4x 4x 4x 1x 1x 3x 1x 1x 2x 2x 144x 1x 1x 1x 1x 1x 1x 144x 2x 2x 2x 144x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 1x 1x 1x 1x 2x 144x | "use client";
import { useState } from "react";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Button } from "@/components/ui/button";
// Form submission API URL
const API_URL = process.env.STRAPI_BASE_URL || "http://localhost:1337";
interface JobApplicationFormProps {
jobId: string;
jobName: string; // New prop for job name
}
const JobApplicationForm = ({ jobId, jobName }: JobApplicationFormProps) => {
const [applicationData, setApplicationData] = useState({
name: "",
email: "",
phone: "",
coverLetter: "",
resume: null as File | null,
});
const [loading, setLoading] = useState(false);
const [message, setMessage] = useState("");
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const { name, value } = e.target;
setApplicationData((prev) => ({ ...prev, [name]: value }));
};
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files ? e.target.files[0] : null;
Eif (file) {
const validFileTypes = ["application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"];
const maxFileSize = 5 * 1024 * 1024; // 5MB in bytes
// Check file type
if (!validFileTypes.includes(file.type)) {
setMessage("Please upload a valid PDF or DOCX file.");
return;
}
// Check file size
if (file.size > maxFileSize) {
setMessage("File size exceeds the 5MB limit. Please upload a smaller file.");
return;
}
// If file is valid, update the state
setApplicationData((prev) => ({ ...prev, resume: file }));
setMessage(""); // Clear any previous messages
}
};
const uploadFile = async (file: File) => {
const formData = new FormData();
formData.append("files", file); // Attach file to FormData
const response = await fetch(`${API_URL}/api/upload`, {
method: "POST",
body: formData, // Send the FormData as the body
});
if (response.ok) {
const responseData = await response.json();
return responseData[0].id; // Get the file ID from the response
} else E{
throw new Error("Failed to upload file");
}
};
const validateForm = () => {
const { name, email, phone, coverLetter, resume } = applicationData;
Iif (!name || !email || !phone || !coverLetter) {
setMessage("Please fill in all the required fields.");
return false;
}
return true;
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setMessage("");
Iif (!validateForm()) {
setLoading(false);
return;
}
let resumeId = null;
try {
if (applicationData.resume) {
// Upload the resume first if it's present
resumeId = await uploadFile(applicationData.resume);
}
const requestBody = {
data: {
name: applicationData.name,
email: applicationData.email,
phone: applicationData.phone,
coverLetter: applicationData.coverLetter,
job: String(jobId) + " - " + jobName, // The job ID this application is associated with
resume: resumeId, // Attach the resume ID (null if no file uploaded)
},
};
const response = await fetch(`${API_URL}/api/job-applications`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody), // Send the application data
});
if (response.ok) {
setMessage("Application submitted successfully!");
} else {
const errorDetails = await response.json();
setMessage("There was an error submitting your application.");
console.error("Error details:", errorDetails);
}
} catch (error) {
setMessage("There was an error submitting your application.");
console.error("Error submitting application:", error);
} finally {
setLoading(false);
}
};
return (
<div className="container mx-auto p-6 mt-6">
<form onSubmit={handleSubmit} className="space-y-4">
<h3 className="text-xl font-semibold font-heading">Apply for this Job</h3>
{message && (
<div className="mt-4 text-center text-red-500">
<p>{message}</p>
</div>
)}
<Input
type="text"
name="name"
placeholder="Your Name"
value={applicationData.name}
onChange={handleInputChange}
required
/>
<Input
type="email"
name="email"
placeholder="Your Email"
value={applicationData.email}
onChange={handleInputChange}
pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
title="Enter a valid email address (e.g., user@example.com)"
required
/>
<Input
type="tel"
name="phone"
placeholder="Your Phone Number"
value={applicationData.phone}
onChange={handleInputChange}
pattern="^\+?[1-9]\d{7,14}$"
title="Enter a valid phone number with at least 8 digits (e.g., +12345678)"
required
/>
<Textarea
name="coverLetter"
placeholder="Cover Letter"
value={applicationData.coverLetter}
onChange={handleInputChange}
required
/>
{/* Modern File Upload Input */}
<div className="space-y-2">
<Label htmlFor="resume" className="text-sm text-gray-700 font-medium">
Upload Resume
</Label>
<div className="flex items-center justify-center w-full">
{/* File Upload Button */}
<label
htmlFor="resume"
className="flex items-center justify-center bg-gray-100 hover:bg-gray-200 border-2 border-dashed border-gray-300 rounded-lg py-4 px-6 cursor-pointer transition duration-200 ease-in-out"
>
<span className="text-sm text-gray-700 font-medium">Choose a File</span>
<input
type="file"
id="resume"
name="resume"
accept=".pdf,.docx"
className="hidden"
onChange={handleFileChange}
/>
</label>
{/* Display file name if a file is selected */}
{applicationData.resume && (
<div className="flex items-center ml-4 text-sm text-gray-600">
<span>{applicationData.resume.name}</span>
</div>
)}
</div>
</div>
<Button type="submit" className="w-full mt-4 font-sans" disabled={loading}>
{loading ? "Submitting..." : "Submit Application"}
</Button>
</form>
</div>
);
};
export default JobApplicationForm;
|